package com.agu.mgt.fsmsmgt.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;

import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.entity.fsmsmgt.FsmsTemplateCanvasEntity;
import com.agu.mgt.fsmsmgt.service.FsmsTemplateCanvasService;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.FormatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 模板区域表
 * 
 * @author 
 * @email 
 * @date 2019-01-18 13:37:03
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("模板区域表")
@RequestMapping("fsmstemplatecanvas")
public class FsmsTemplateCanvasController extends AbstractController{
	@Autowired
	private FsmsTemplateCanvasService fsmsTemplateCanvasService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("fsmstemplatecanvas:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<FsmsTemplateCanvasEntity> page = fsmsTemplateCanvasService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{canvasId}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("fsmstemplatecanvas:info")
	public Result info(@PathVariable("canvasId") Integer canvasId){
		FsmsTemplateCanvasEntity fsmsTemplateCanvas = fsmsTemplateCanvasService.queryObject(canvasId);
		
		return ResultGenerator.successData(fsmsTemplateCanvas);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增模板区域表")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("fsmstemplatecanvas:save")
	public Result save(@RequestBody FsmsTemplateCanvasEntity fsmsTemplateCanvas){
	
		fsmsTemplateCanvasService.save(fsmsTemplateCanvas);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("canvasId","模板ID");
		     cmap.put("templateId","模板ID");
		     cmap.put("canvasType","区域类别");
		     cmap.put("canvasSort","排序");
		     cmap.put("content","内容");
		     cmap.put("contentFlag","内容标识");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(fsmsTemplateCanvas, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改模板区域表")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("fsmstemplatecanvas:update")
	public Result update(@RequestBody FsmsTemplateCanvasEntity fsmsTemplateCanvas){
	
		FsmsTemplateCanvasEntity fsmsTemplateCanvas1 = fsmsTemplateCanvasService.queryObject(fsmsTemplateCanvas.getCanvasId());
	
		fsmsTemplateCanvasService.update(fsmsTemplateCanvas);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("canvasId","模板ID");
		     cmap.put("templateId","模板ID");
		     cmap.put("canvasType","区域类别");
		     cmap.put("canvasSort","排序");
		     cmap.put("content","内容");
		     cmap.put("contentFlag","内容标识");
		     cmap.put("createTime","创建时间");
	
		String remark = CompareUtils.getTwoObjectRemark(fsmsTemplateCanvas1, fsmsTemplateCanvas, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除模板区域表")
	@GetMapping("/delete/{canvasId}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("fsmstemplatecanvas:delete")
	public Result delete(@PathVariable("canvasId") Integer canvasId){
	
	    	if (canvasId == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		FsmsTemplateCanvasEntity fsmsTemplateCanvas = fsmsTemplateCanvasService.queryObject(canvasId);
	
		fsmsTemplateCanvasService.delete(canvasId);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("canvasId","模板ID");
		     cmap.put("templateId","模板ID");
		     cmap.put("canvasType","区域类别");
		     cmap.put("canvasSort","排序");
		     cmap.put("content","内容");
		     cmap.put("contentFlag","内容标识");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(fsmsTemplateCanvas, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除模板区域表")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("fsmstemplatecanvas:delete")
	public Result deletes(@RequestBody Integer[] canvasIds){
	
		fsmsTemplateCanvasService.deleteBatch(canvasIds);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("fsmstemplatecanvas:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<FsmsTemplateCanvasEntity> list = fsmsTemplateCanvasService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

               Map<Integer, String> contentFlagMap = new HashMap<Integer, String>();

                contentFlagMap.put(1, "模板提供");
                contentFlagMap.put(2, "发送时提供");
            map.put("contentFlagMap", contentFlagMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("模板ID", "canvasId", 1);
			clist.add(cloumn);
            cloumn = new Column("模板ID", "templateId", 1);
			clist.add(cloumn);
            cloumn = new Column("区域类别", "canvasType", 1);
			clist.add(cloumn);
            cloumn = new Column("排序", "canvasSort", 1);
			clist.add(cloumn);
            cloumn = new Column("内容", "content", 1);
			clist.add(cloumn);
            cloumn = new Column("内容标识", "contentFlag", 2);
			clist.add(cloumn);
            cloumn = new Column("创建时间", "createTime", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
